#!/bin/bash

TERM=/bin/bash

if [ "$2" = "" -o "$3" = "" ]
then
        echo -e "Usage: $0 -np <procs> <command> \nex: $0 -np 8 runme\n"
        exit 0
fi

echo "#!/bin/bash" > run.sh
echo "#$ -cwd" >> run.sh
echo '/opt/openmpi/bin/mpirun -n $NSLOTS'" $(pwd)/$3" >> run.sh

qsub -pe orte $2 run.sh

out=$(qstat)
gout=$(echo "$out" | grep $(whoami))

echo "Running..."

while [ "$gout" != "" ]
do
        #clear
        echo $gout
        out=$(qstat)
        gout=$(echo "$out" | grep $(whoami))
        sleep 1
done

echo -e "\nResults:\n"
cat run.sh.*
rm -f run.sh.*
rm -f run.sh
